package com.dh.api.gateway.filter;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

/** 
* @author 作者 dinghao
* @version 创建时间：2018年11月20日 上午11:22:54 
* @Description 类说明 : token 校验(从请求的参数中获取)
*/
@Component
public class TokenFilter extends ZuulFilter{

	@Override
	public String filterType() {
		return PRE_TYPE;
	}
	
	@Override
	public int filterOrder() {
		return PRE_DECORATION_FILTER_ORDER -1;
	}
	
	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public Object run(){
		RequestContext requestContext = RequestContext.getCurrentContext();
		HttpServletRequest request = requestContext.getRequest();
		
		//这里从参数里获取,也可以从Cookie,header里获取
		String token = request.getParameter("token");
		if(StringUtils.isEmpty(token)) {
//			requestContext.setSendZuulResponse(false);
//			requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
		}
		
		return null;
	}



}
